import { Request, Response } from 'express';
import moment from 'moment';

export default {
  'GET /_api/produce-dashboard/getLayout': getLayout,
  'GET /_api/produce-dashboard/getTodayProduce': getTodayProduce,
  'GET /_api/produce-dashboard/getTodayPass': getTodayPass,
  'GET /_api/produce-dashboard/getTodayWork': getTodayWork,
  'GET /_api/produce-dashboard/getOee': getOee,
  'GET /_api/produce-dashboard/getCoreMaterials': getCoreMaterials,
  'GET /_api/produce-dashboard/getExceptionMessage': getExceptionMessage,
  'GET /_api/produce-dashboard/getAnalysis': getAnalysis,
};

const layout = [
  {
    sourceCode: 'TodayOutput',
    w: 3,
    h: 3,
    x: 0,
    y: 0,
  },
  {
    sourceCode: 'TodayPass',
    w: 3,
    h: 3,
    x: 3,
    y: 0,
  },
  {
    sourceCode: 'TodayWork',
    w: 3,
    h: 3,
    x: 6,
    y: 0,
  },
  {
    sourceCode: 'TodayOee',
    w: 3,
    h: 3,
    x: 9,
    y: 0,
  },
  {
    sourceCode: 'CoreMaterials',
    w: 12,
    h: 7,
    x: 0,
    y: 4,
  },
  {
    sourceCode: 'ExceptionMessage',
    w: 6,
    h: 10,
    x: 0,
    y: 10,
  },
  {
    sourceCode: 'YieldAnalysis',
    w: 6,
    h: 10,
    x: 8,
    y: 10,
  },
];

function getLayout(_req: Request, res: Response) {
  return res.json(layout);
}

function getTodayProduce(_req: Request, res: Response) {
  const result = {
    title: '今日产量',
    number: 8540,
    total: 9700,
    desc: '今日成产进度',
    status: '',
    percent: '88.3%',
  };
  return res.json(result);
}

function getTodayPass(_req: Request, res: Response) {
  const result = {
    color: '#39C878',
    title: '今日一次通过率',
    number: '97.3%',
    total: '',
    desc: '同比上周',
    status: 'up',
    percent: '8.1%',
  };
  return res.json(result);
}

function getTodayWork(_req: Request, res: Response) {
  const result = {
    color: '#F8B303',
    title: '今日出勤 (人)',
    number: 325,
    total: '',
    desc: '与上周持平',
    status: '',
    percent: '',
  };
  return res.json(result);
}

function getOee(_req: Request, res: Response) {
  const result = {
    color: '#FD6868',
    title: 'OEE',
    number: '59.3%',
    total: '',
    desc: '同比上周',
    status: 'down',
    percent: '2.4%',
  };
  return res.json(result);
}

function getCoreMaterials(_req: Request, res: Response) {
  const beginDay = new Date().getTime();
  const visitData1 = [] as any;
  const fakeY1 = [1, 6, 4, 8, 3, 7, 2];
  for (let i = 0; i < fakeY1.length; i += 1) {
    visitData1.push({
      x: moment(new Date(beginDay + 1000 * 60 * 60 * 24 * i)).format('YYYY-MM-DD'),
      y: fakeY1[i],
    });
  }
  const visitData2 = [] as any;
  const fakeY2 = [3, 9, 5, 2, 7, 6, 1];
  for (let i = 0; i < fakeY2.length; i += 1) {
    visitData2.push({
      x: moment(new Date(beginDay + 1000 * 60 * 60 * 24 * i)).format('YYYY-MM-DD'),
      y: fakeY2[i],
    });
  }
  const visitData3 = [] as any;
  const fakeY3 = [2, 8, 4, 1, 6, 5, 9];
  for (let i = 0; i < fakeY3.length; i += 1) {
    visitData3.push({
      x: moment(new Date(beginDay + 1000 * 60 * 60 * 24 * i)).format('YYYY-MM-DD'),
      y: fakeY3[i],
    });
  }
  const result = [
    {
      name: '圆锅',
      unit: '吨',
      number: '1200',
      status: 'up',
      bizData: visitData1,
      percent: '8.1',
      borderColor: '#4F7DE7',
      color: 'rgba(79,125,231, 0.1)',
    },
    {
      name: '齿轮精品加工',
      unit: '件',
      number: '68021',
      status: 'up',
      bizData: visitData2,
      percent: '18',
      borderColor: '#39C878',
      color: 'rgba(57,200,120, 0.1)',
    },
    {
      name: '成品',
      unit: '台',
      number: '16324',
      status: 'down',
      bizData: visitData3,
      percent: '10',
      borderColor: '#F8B303',
      color: 'rgba(248,179,3, 0.1)',
    },
  ];
  return res.json(result);
}

function getExceptionMessage(_req: Request, res: Response) {
  const result = [
    {
      type: 'error',
      name: '机加工轴14数控磨床',
      reason: '三号润滑点油量偏低',
      date: '一分钟前',
    },
    {
      type: 'error',
      name: '机加工轴14数控磨床',
      reason: '三号润滑点油量偏低',
      date: '三分钟前',
    },
    {
      type: 'error',
      name: '机加工轴14数控磨床',
      reason: '三号润滑点油量偏低',
      date: '五分钟前',
    },
    {
      type: 'error',
      name: '机加工轴14数控磨床',
      reason: '三号润滑点油量偏低',
      date: '一分钟前',
    },
    {
      type: 'error',
      name: '机加工轴14数控磨床',
      reason: '三号润滑点油量偏低',
      date: '一分钟前',
    },
    {
      type: 'error',
      name: '机加工轴14数控磨床',
      reason: '三号润滑点油量偏低',
      date: '一分钟前',
    },
    {
      type: 'warning',
      name: '机加工轴14数控磨床',
      reason: '三号润滑点油量偏低',
      date: '一小时前',
    },
    {
      type: 'warning',
      name: '机加工轴14数控磨床',
      reason: '三号润滑点油量偏低',
      date: '一小时前',
    },
    {
      type: 'normal',
      name: '机加工轴14数控磨床',
      reason: '三号润滑点油量偏低',
      date: '五小时前',
    },
    {
      type: 'normal',
      name: '机加工轴14数控磨床',
      reason: '三号润滑点油量偏低',
      date: '六小时前',
    },
  ];
  return res.json(result);
}

function getAnalysis(_req: Request, res: Response) {
  const result = [
    { name: '不良品计数', 名称: 'RS系列', 数值: 587 },
    { name: '不良品计数', 名称: 'LT系列', 数值: 123 },
    { name: '不良品计数', 名称: 'S系列', 数值: 187 },
    { name: '不良品计数', 名称: 'A系列', 数值: 12 },
    { name: '不良品计数', 名称: 'B系列', 数值: 128 },
    { name: '不良品计数', 名称: 'C系列', 数值: 65 },
    { name: '不良品计数', 名称: 'D系列', 数值: 87 },
    { name: '未完成数', 名称: 'RS系列', 数值: 123 },
    { name: '未完成数', 名称: 'LT系列', 数值: 123 },
    { name: '未完成数', 名称: 'S系列', 数值: 187 },
    { name: '未完成数', 名称: 'A系列', 数值: 12 },
    { name: '未完成数', 名称: 'B系列', 数值: 128 },
    { name: '未完成数', 名称: 'C系列', 数值: 65 },
    { name: '未完成数', 名称: 'D系列', 数值: 87 },
  ];
  return res.json(result);
}
